// pages/order/order.js
var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var pay = require('../../utils/pay');
var storage = require('../../utils/storage');

var pageSetting = {
    data: {
        modalHidden: true,
        disabled:false,
        offlineSalesForm:''
    },
    onLoad:function(options){
      app.globalData.cartCountState = true;
      app.globalData.cartNumState = true;
      app.globalData.cartDetailCount = true;
      var that = this;
      var orderId = options.order_id;
      if(options.offlineSalesForm){
        var offlineSalesForm = options.offlineSalesForm
      that.setData({
         offlineSalesForm:offlineSalesForm[0]
      })
      }
      

      var data={
            'self:oc:order:getOrderDetail':{
                'order_id':orderId
            }
        }
        request.post({
          path: '/wxapp_api/api'
        },data)
            .then(function(res){
              var retData = res['self:oc:order:getOrderDetail'];
              if(retData.ret != 0){
                  modal.toast(retData.msg);
                  return false;
              }  

              var data = retData['data'];
              data.price = retData['data']['price'] / 100;
              if(retData['data']['shipping'] != undefined){
                  data.freight = retData['data']['shipping'] / 100;
              }else{
                  data.freight = 0;
              }
              data.total = retData['data']['total'] / 100;
 
              that.setData({
                  orderData:data
              });
            }); 
    },
    callwxPay:function(e){
        var orderId = e.target.dataset.orderid;
        var that = this;
        that.setData({
            disabled:true
        });
        pay.callPay({
            'order_id':orderId,
            success:function(res){

                wx.redirectTo({
                    url: '../payment-success/success?order_id='+orderId+'&offlineSalesForm='+that.data.offlineSalesForm
                })
            },
            fail:function(res){
                if (res.errMsg == "requestPayment:fail cancel"){
                    wx.navigateBack({
                        delta: 1
                    })
                    return false;
                }           
                wx.redirectTo({
                    url: '../payment-error/error?order_id='+orderId
                }) 
            }
        });
    }
};
utils.assign(pageSetting);
Page(pageSetting);